package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.fault.tripping.TrippingDetailManager;
import com.sduept.nwld.dataserver.manager.protection.EquipmentTripDetailManager;
import com.sduept.nwld.dataserver.model.daily.Tripping;
import com.sduept.nwld.dataserver.model.tripping.YxBwDetailDTO;
import com.sduept.nwld.dataserver.model.tripping.YxSoeDeatilDTO;
import com.sduept.utils.DateUtil;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Named
@ViewScoped
public class EquipmentTripDetailController extends AbstractController {
	private static final long serialVersionUID = 6484247512125126810L;
	@Autowired
	private EquipmentTripDetailManager detailManager;
	@Autowired
	private CimManager cimM;
	@Autowired
	private TrippingDetailManager tripManager;

	private List<Tripping> details = new ArrayList<Tripping>();

	private LazyDataModel<Tripping> model;

	private Tripping selectTrip;

	private Tripping current = new Tripping();

	private List<String> voltageClasses = new ArrayList<String>();
	private String voltage;
	private List<String> switchNames = new ArrayList<String>();
	private String switchName;
	private List<String> lineNames = new ArrayList<String>();
	private String lineName;
	private Date queryDate1;
	private Date queryDate2;

	private int sort;// 排序所用

	private String startStation;
	private String endStation;
	private String lineId;
	private List<YxBwDetailDTO> BWList1 = null;
	private List<YxBwDetailDTO> BWList2 = null;
	private List<YxSoeDeatilDTO> SOEList1 = null;
	private List<YxSoeDeatilDTO> SOEList2 = null;

	@PostConstruct
	public void init() {
		initTable();
		initVoltage();
		initSwitchNames();
		initLineName();
	}

	public void reset() {
		queryDate1 = null;
		queryDate2 = null;
		switchName = "";
		lineName = "";
		voltage = "";
		initTable();
	}

	public void initTable() {
		final String start;
		final String end;
		if (null != queryDate1) {
			start = DateUtil.getStringDate(queryDate1, DateUtil.pattern_ymd) + " 00:00:00";
		} else {
			start = null;
		}
		if (null != queryDate2) {
			end = DateUtil.getStringDate(queryDate2, DateUtil.pattern_ymd) + " 23:59:59";
		} else {
			end = null;
		}
		if (null != queryDate1 && null != queryDate2 && queryDate1.getTime() > queryDate2.getTime()) {
			addErrorMessage("开始时间不能大于结束时间！");
			return;
		}
		model = new LazyDataModel<Tripping>() {
			private static final long serialVersionUID = 2060259946172703357L;

			@Override
			public List<Tripping> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				sort = first;
				details = detailManager.listTrippingWithRange(start, end, switchName, lineName, voltage, new int[] { first, first + pageSize });
				return details;
			}

			@Override
			public List<Tripping> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
				sort = first;
				details = detailManager.listTrippingWithRange(start, end, switchName, lineName, voltage, new int[] { first, first + pageSize });
				return details;
			}

			@Override
			public Tripping getRowData(String rowKey) {
				for (Tripping t : details) {
					if (t.getId().equals(rowKey)) {
						return t;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(Tripping object) {
				for (Tripping t : details) {
					if (t.getId().equals(object.getId())) {
						return t;
					}
				}
				return null;
			}
		};
		int recordSize = detailManager.countRecords(start, end, switchName, lineName, voltage);
		model.setRowCount(recordSize);
	}

	public void preCreate() {
		lineId = "";
		startStation = "";
		endStation = "";
		current = new Tripping();
	}

	public void preUpate() {
		current = selectTrip;
	}

	public void save() {
		current.setStationName1(startStation);
		current.setStationName2(endStation);
		current.setLineId(lineId);
		detailManager.saveTripping(current);
		initTable();
		addSuccessMessage("保存成功");
	}

	public void delete() {
		String id = selectTrip.getId();
		detailManager.deleteById(id);
		initTable();
		selectTrip = null;
		addSuccessMessage("删除成功");
	}

	public void initVoltage() {
		voltageClasses = detailManager.getAllVoltage();
	}

	public void initSwitchNames() {
		switchNames = detailManager.getAllSwitchNames();
	}

	public void initLineName() {
		lineNames = detailManager.getAllLineNames();
	}

	public List<String> completeVoltageClass(String query) {
		initVoltage();
		return voltageClasses;
	}

	public List<String> completeSwitchName(String query) {
		initSwitchNames();
		List<String> results = new ArrayList<String>();
		if (switchNames.size() > 0) {
			for (String switchName : switchNames) {
				if (StringUtils.isNotEmpty(switchName)) {
					String header = PingYinUtil.getPingYinHeader(switchName);
					if (header.contains(query)) {
						results.add(switchName);
					}
				}
			}
		}
		return results;
	}

	public List<String> completeLineName(String query) {
		initLineName();
		List<String> results = new ArrayList<String>();
		if (lineNames.size() > 0) {
			for (String lineName : lineNames) {
				String header = PingYinUtil.getPingYinHeader(lineName);
				if (header.contains(query)) {
					results.add(lineName);
				}
			}
		}
		return results;
	}

	public void autoCompleteLineStation(SelectEvent event) {
		Object item = event.getObject();
		if (null != item) {
			getStationAndIdByLineName(item.toString());
		}
	}

	public void getStationAndIdByLineName(String lineName) {
		lineId = "";
		startStation = "";
		endStation = "";
		List<ACLineSegment> lines = cimM.getAcLineSegmentsByName(lineName);
		if (lines.size() != 0) {
			ACLineSegment line = lines.get(0);
			startStation = line.getStartStName();
			endStation = line.getEndStName();
			lineId = line.getId();
		}
	}

	public void getYXDetail(Tripping selectTrip) {
		setSelectTrip(selectTrip);
		Date time = selectTrip.getTripTime();
		String[] subtationNames = { selectTrip.getStationName1(), selectTrip.getStationName2() };
		Map<String, String> map = tripManager.getFaceIds(subtationNames);
		Map<String, List<YxBwDetailDTO>> BWMap = tripManager.getBXDetail(time, subtationNames, map);
		Map<String, List<YxSoeDeatilDTO>> SOEMap = tripManager.getSOEDetail(time, subtationNames, map);
		BWList1 = BWMap.get(selectTrip.getStationName1());
		BWList2 = BWMap.get(selectTrip.getStationName2());
		SOEList1 = SOEMap.get(selectTrip.getStationName1());
		SOEList2 = SOEMap.get(selectTrip.getStationName2());
	}

	public void onRowSelect(SelectEvent event) {
		selectTrip = (Tripping) event.getObject();
	}

	public int getIndex(Tripping detail) {
		return details.indexOf(detail) + sort + 1;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public Date getQueryDate1() {
		return queryDate1;
	}

	public void setQueryDate1(Date queryDate1) {
		this.queryDate1 = queryDate1;
	}

	public Date getQueryDate2() {
		return queryDate2;
	}

	public void setQueryDate2(Date queryDate2) {
		this.queryDate2 = queryDate2;
	}

	public List<Tripping> getDetails() {
		return details;
	}

	public void setDetails(List<Tripping> details) {
		this.details = details;
	}

	public Tripping getSelectTrip() {
		return selectTrip;
	}

	public void setSelectTrip(Tripping selectTrip) {
		this.selectTrip = selectTrip;
	}

	public Tripping getCurrent() {
		return current;
	}

	public void setCurrent(Tripping current) {
		this.current = current;
	}

	public LazyDataModel<Tripping> getModel() {
		return model;
	}

	public void setModel(LazyDataModel<Tripping> model) {
		this.model = model;
	}

	public List<String> getVoltageClasses() {
		return voltageClasses;
	}

	public void setVoltageClasses(List<String> voltageClasses) {
		this.voltageClasses = voltageClasses;
	}

	public List<String> getSwitchNames() {
		return switchNames;
	}

	public void setSwitchNames(List<String> switchNames) {
		this.switchNames = switchNames;
	}

	public List<String> getLineNames() {
		return lineNames;
	}

	public void setLineNames(List<String> lineNames) {
		this.lineNames = lineNames;
	}

	public String getVoltage() {
		return voltage;
	}

	public void setVoltage(String voltage) {
		this.voltage = voltage;
	}

	public String getSwitchName() {
		return switchName;
	}

	public void setSwitchName(String switchName) {
		this.switchName = switchName;
	}

	public String getLineName() {
		return lineName;
	}

	public void setLineName(String lineName) {
		this.lineName = lineName;
	}

	public String getStartStation() {
		return startStation;
	}

	public void setStartStation(String startStation) {
		this.startStation = startStation;
	}

	public String getEndStation() {
		return endStation;
	}

	public void setEndStation(String endStation) {
		this.endStation = endStation;
	}

	public String getLineId() {
		return lineId;
	}

	public void setLineId(String lineId) {
		this.lineId = lineId;
	}

	public List<YxBwDetailDTO> getBWList1() {
		return BWList1;
	}

	public void setBWList1(List<YxBwDetailDTO> bWList1) {
		BWList1 = bWList1;
	}

	public List<YxBwDetailDTO> getBWList2() {
		return BWList2;
	}

	public void setBWList2(List<YxBwDetailDTO> bWList2) {
		BWList2 = bWList2;
	}

	public List<YxSoeDeatilDTO> getSOEList1() {
		return SOEList1;
	}

	public void setSOEList1(List<YxSoeDeatilDTO> sOEList1) {
		SOEList1 = sOEList1;
	}

	public List<YxSoeDeatilDTO> getSOEList2() {
		return SOEList2;
	}

	public void setSOEList2(List<YxSoeDeatilDTO> sOEList2) {
		SOEList2 = sOEList2;
	}

}
